import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import test1.bean.Test1;
import test1.dao.Test1Mapper;
import test1.util.WhereParams;
import user.bean.User;
import user.dao.UserMapper;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class studyDayDayDay {
    @Test
    public void write(){
        //哎，，来吧，，学吧，天天学吧，那能咋办啊，成硬着头皮学了
        //今天主要讲了多对多的连表查询，多对多的查询其实也就是一种复杂的一对多

        //然后今天主要的精力讲了动态sql的相关内容，分别讲了如下这些标签
        /*
        * <where>标签，<where>标签可以去除where中多余的and符号，然后如果where中没有元素
        * 则where不渲染。
        *
        * <if>标签，如果结果为true则执行，如果为false则不执行标签内的内容
        * if标签有属性test，然后test中大于和小于要用转移符号&gt &lt
        *
        * choose-when-otherwise标签类似于if-else标签，用法就是
        * <choose>
        *   <when test="条件">
        *   </when>
        *   <otherwise>
        *    </otherwise>
        *   </choose>
        * sql-include 对sql语句的封装
        * <sql id="xxx">xxxxxxxx</sql>
        * <include reifd="xxx"/>
        *
        * foreach 对目标元素进行循环渲染
        * 属性有collection：需要遍历的数据类型或名称
        * item：自定义的名称
        * open：前缀
        * close：后缀
        * separator 每两个元素之间的补充字符
        * <foreach collection="" item="" open close=" " separator="">
        </foreach>
        *
        * useGeneratedKeys
        * */

        //差不多了，练习练习写作业了
    }
    static SqlSessionFactory sqlSessionFactory;
    SqlSession sqlSession;
    @BeforeClass
    public static void ready() throws IOException {
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
    }

    @Test
    public void useWhere(){
        sqlSession=sqlSessionFactory.openSession();
        Test1Mapper test1Mapper=sqlSession.getMapper(Test1Mapper.class);
        //
        List<Test1> test1List=test1Mapper.selectTest1UseWhere(new WhereParams[]{new WhereParams("id",new int[]{1,2,3,4,5}),new WhereParams("count",new int[]{11,333})});
        System.out.println(test1List);


    }

    //这个是执行GeneratedKeys的练习
    @Test
    public void useGenerateKeys(){
        sqlSession=sqlSessionFactory.openSession();
        Test1 test1=new Test1("新的新的",100);
        Test1Mapper test1Mapper=sqlSession.getMapper(Test1Mapper.class);
        System.out.println(test1Mapper.InsertTest1Item(test1));
        System.out.println(test1);
    }

    //写完运行一下吧
    @Test
    public void homework() throws IOException {
        sqlSession=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config-cskaoyan.xml")).openSession();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        List<User> userList=userMapper.selectUserByAge(25);
        System.out.println(userList);
    }

    @After
    public void close(){
        if (sqlSession!=null){
            sqlSession.close();
        }
    }
}
